package cn.wy.basic.controller;

import cn.wy.basic.dto.LoginDto;
import cn.wy.basic.util.AjaxResult;
import cn.wy.exception.BusinessException;
import cn.wy.user.service.LogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private LogininfoService logininfoService;
    @PostMapping ("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto) {
        try {
            Map<String,Object > map = logininfoService.accountLogin(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统错误，请稍后再试！");
        }
    }

    /**
     * 微信登录接口
     * @param code
     * @return
     */
    @GetMapping ("/wechat/{code}")
    public AjaxResult wechatLogin(@PathVariable("code")  String code){
        try {
            return logininfoService.wechatLogin(code);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    /**
     * 微信绑定接口
     * @param map
     * @return
     * map【map只能是一种，其他类型需要手动转换】  和    dto【为每一个数据定义类型】，需要定义一个dto
     */
    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody Map<String,String> map){
        try {
            return logininfoService.wechatBinder(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }
}
